/**
 *
 * 建造工具icon
 * @author 
 * 
 */
class UnitIcon extends egret.Sprite{
    
    /**类名*/
    public className: string;
    /**价格*/
    public price: number;
    /**玩家拥有金钱*/
    private gold: number;
    
    /**蒙板*/
    protected mb: egret.Bitmap;
    /**CD*/
    protected touchTime: number = 0;
    private istouch:boolean = false;
    
    private coldDown: egret.Bitmap;
    private balance: egret.Bitmap;
    private icon: egret.Bitmap;
    private priceTxt: egret.TextField = new egret.TextField;
    private res :any;
    
    //属性
    private fireDelay:number;
    private specialType:number;
    private unitType: number;
    private life: number;
    private damage: number;
    private attackType:number;
    private maxSpeed: number;
    
    private label: string;
    private info: string;
    private unitTip: string;
    private tip: string;
    
    private touchPointX:number=0;
    private touchPointY:number=0;

    
	public constructor(towerName:string,gold:number) {
        super();
        this.gold = gold;
        if(towerName == "lock"){
            this.icon = Utils.createBitmapByName("unitLock");
            this.addChild(this.icon);           
            return;
        }
        
        this.className = TowerLevel.soldierData[towerName].type;//获取
        this.res = towerName;//获取资源名
        this.price = TowerLevel.soldierData[towerName].price;//获取价格
        this.specialType = TowerLevel.soldierData[towerName].specialType;//获取特殊类型
        this.unitType = TowerLevel.soldierData[towerName].unitType;//获取单位类型
        this.life = TowerLevel.soldierData[towerName].life;//获取生命值
        this.damage = TowerLevel.soldierData[towerName].damage;//获取伤害
        this.fireDelay = TowerLevel.soldierData[towerName].fireDelay;//获取攻击速度
        this.attackType = TowerLevel.soldierData[towerName].attackType;//获取攻击类型
        this.maxSpeed = TowerLevel.soldierData[towerName].maxSpeed;//获取攻击类型
        this.label = TowerLevel.soldierData[towerName].label;
        this.info = TowerLevel.soldierData[towerName].info;
        this.unitTip = TowerLevel.soldierData[towerName].unitTip;
        this.tip = TowerLevel.soldierData[towerName].tip;

        
        //icon
        this.icon = Utils.createBitmapByName(this.res);
        this.addChild(this.icon);
        this.icon.x = -10;
        this.icon.y=-14;
        
        this.balance = Utils.createBitmapByName("mb");
        this.addChild(this.balance);
        
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchHandle,this);
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this);

        //text
        this.priceTxt.text = this.price.toString();
        this.priceTxt.textColor = 0xffff00;
        this.priceTxt.size = 10;
        this.priceTxt.fontFamily = "Microsoft YaHei";
        this.priceTxt.anchorOffsetX =this.priceTxt.width;
        this.priceTxt.x = 45;
        this.priceTxt.y = 32;
        this.addChild(this.priceTxt);
        
        if(this.gold>=this.price){
            this.balance.height = 0;
        }
        
	}
	
    /**点击事件*/
    protected touchHandle(e: egret.TouchEvent) {
        //选中音效
        GuankaBase.instance.hideTool(null);
        this.istouch = true;
        SoundManager.playEffect("select");
        if(this.gold>=this.price){
            //Group.selectIcon(this); 
            this.touchPointX = e.stageX;
            this.touchPointY = e.stageY;
            this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.mouseMove,this);

        }
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this);
    }
    
    protected mouseMove(e: egret.TouchEvent) {
        this.istouch = false;
        this.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.mouseEnd,this);
        this.removeEventListener(egret.TouchEvent.TOUCH_TAP,this.touch,this);
        this.icon.x = this.icon.x + (e.stageX - this.touchPointX);
        this.icon.y = this.icon.y + (e.stageY - this.touchPointY);
        GuankaBase.instance.shape.alpha=0.2;
        this.touchPointX = e.stageX;
        this.touchPointY = e.stageY;
        GuankaBase.instance.guankaUI.boardSp.visible=false;
        
    }
    protected mouseEnd(e: egret.TouchEvent) {
        GuankaBase.instance.shape.alpha = 0;
        //this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.selectIcon,this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.mouseMove,this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.mouseEnd,this);
        this.icon.x = -10;
        this.icon.y = -14;
        this.touchPointX = 0;
        this.touchPointY = 0;
        if(e.stageX > 30 && e.stageX < 450 && e.stageY >360&&e.stageY<530){
            var x=e.stageX;
            var y =e.stageY;
            if(Math.abs(x - 240) < 40 && Math.abs(y - 550)<40){
                if(x>=240){
                    x=290;
                }else{
                    x=190
                }
            }
            if(Math.abs(x - 120) < 40 && Math.abs(y - 490) < 40) {
                if(x >=120) {
                    x = 160;
                } else {
                    x = 80
                }
            }
            if(Math.abs(x - 360) < 40 && Math.abs(y - 490) < 40) {
                if(x >= 360) {
                    x = 400;
                } else {
                    x = 320
                }
            }
            GuankaBase.instance.gold -= this.price;
            GuankaBase.instance.guankaUI.setGold(GuankaBase.instance.gold);
            GuankaBase.instance.addSoldier(this.res,x,y);
        }
    }

    public touch(e: egret.TouchEvent) {
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.mouseMove,this);
        this.stage.removeEventListener(egret.TouchEvent.TOUCH_END,this.mouseEnd,this);
        this.istouch = false;
        if(this.touchTime <= 400 && this.touchTime!=0){
            Group.selectIcon(this);
        }
        this.touchTime = 0;


    }
    
    public selectIcon(e: egret.TouchEvent){
        this.icon.y = -24;   
        GuankaBase.instance.shape.alpha = 0.2;
        GuankaBase.instance.guankaUI.boardSp.visible = false;
    }
    public reselectIcon() {
            Group.disposeIcon();
    }

    public deselectIcon() {
        GuankaBase.instance.shape.alpha = 0;
        this.icon.y=-14; 
    }
    /**设置蒙板高度*/
    protected setMbheight(num: number) {
        this.mb.height = num;
    }
    
    /**设置蒙板高度*/
    protected setAlpha(num: number) {
        this.balance.height = num;
    }
    /**帧事件*/
    public onEnterFrame(advancedTime: number) {

        this.gold = GuankaBase.instance.gold;
        if(this.gold >= this.price&&GuankaBase.instance.boing==false) {
            this.setAlpha(0);
            this.touchEnabled = true;
        }else{
            this.setAlpha(46);
            this.touchEnabled = false;
        }
        if(this.istouch) {
            this.touchTime += advancedTime;
            if(this.touchTime > 400) {
                Group.disposeIcon();
                var board = GuankaBase.instance.guankaUI.boardSp;
                board.visible = true;
                //GuankaBase.instance.removeChild(board);
                GuankaBase.instance.guankaUI.label.text = this.label;
                GuankaBase.instance.guankaUI.type.text = this.unitTip;
                GuankaBase.instance.guankaUI.info.text = this.info;
                GuankaBase.instance.guankaUI.tip.text = this.tip;
                GuankaBase.instance.guankaUI.lifetxt.text = (Math.floor(this.life * (1 + 0.2 * GuankaBase.instance.lifeUp))).toString();
                GuankaBase.instance.guankaUI.damagetxt.text = (Math.floor(this.damage * (1 + 0.2 * GuankaBase.instance.damagekUp))).toString();
                if(this.touchPointX > 86) {
                    board.x = this.touchPointX - 86;
                } else {
                    board.x = 10
                }
                board.y = 460;
                this.istouch = false
                this.touchTime = 0;
            }

        }
    }
}
